import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import CreateApp from '@/views/CreateApp.vue'

// 路由守卫：检查是否需要登录
const requireAuth = (to: any, from: any, next: any) => {
  const tokenName = localStorage.getItem('tokenName')
  const tokenValue = localStorage.getItem('tokenValue')
  if (tokenName && tokenValue) {
    next()
  } else {
    next('/login')
  }
}

const requireAdmin = (to: any, from: any, next: any) => {
  const tokenName = localStorage.getItem('tokenName')
  const tokenValue = localStorage.getItem('tokenValue')
  const userInfoStr = localStorage.getItem('userInfo')
  const userInfo = userInfoStr ? JSON.parse(userInfoStr) : null
  if (tokenName && tokenValue && userInfo?.userRole === 'admin') {
    next()
  } else {
    next('/login')
  }
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {
        title: '用户登录'
      }
    },
    {
      path: '/create-app',
      name: 'create-app',
      component: CreateApp,
      beforeEnter: requireAuth,
      meta: {
        title: '创建应用',
        requireAuth: true
      }
    },
    {
      path: '/my-apps',
      name: 'my-apps',
      component: () => import('@/views/MyApps.vue'),
      beforeEnter: requireAuth,
      meta: {
        title: '我的应用',
        requireAuth: true
      }
    },
    {
      path: '/app-market',
      name: 'app-market',
      component: () => import('@/views/AppMarket.vue'),
      meta: {
        title: '应用市场'
      }
    },
    {
      path: '/api-docs',
      name: 'api-docs',
      component: () => import('@/views/ApiDocs.vue'),
      meta: {
        title: 'API文档'
      }
    },
    {
      path: '/ai-generate',
      name: 'ai-generate',
      component: () => import('@/views/AiGenerate.vue'),
      beforeEnter: requireAuth,
      meta: {
        title: 'AI页面生成',
        requireAuth: true
      }
    },
    {
      path: '/workflow-generate',
      name: 'workflow-generate',
      component: () => import('@/views/WorkflowGenerate.vue'),
      beforeEnter: requireAuth,
      meta: {
        title: 'AI工作流生成',
        requireAuth: true
      }
    },
    {
      path: '/personal-library',
      name: 'personal-library',
      component: () => import('@/views/PersonalLibrary.vue'),
      beforeEnter: requireAuth,
      meta: {
        title: '个人图书馆',
        requireAuth: true
      }
    },
    // 管理员违规查看页面
    {
      path: '/admin/violations',
      name: 'admin-violations',
      component: () => import('@/views/AdminViolations.vue'),
      beforeEnter: requireAdmin,
      meta: {
        title: '违规统计',
        requireAuth: true
      }
    }
  ],
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - AI Market`
  } else {
    document.title = 'AI Market'
  }
  
  next()
})

export default router
